package cz.muni.fi.pa165.bean;

import cz.muni.fi.pa165.dto.EmployeeDTO;
import cz.muni.fi.pa165.enums.AdminRole;
import cz.muni.fi.pa165.service.local.impl.EmployeeService;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author shifty
 */
@RequestScoped
@Named
public class EmployeeManager implements Serializable{
    
    @Inject
    EmployeeService employeeService;
    
    private Long id;
    private String name;
    private String birthDate;
    private String role;
    private String password;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    
    public String getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(String birthDate) {
        this.birthDate = birthDate;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }
    
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    
    public void updateEmployee() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
        Date date =sdf.parse((String)birthDate);
        EmployeeDTO employee = new EmployeeDTO();
        employee.setId(id);
        employee.setName(name);
        employee.setBirthDate(date);
        employee.setRole(AdminRole.valueOf(role));
        employee.setPassword(password);
        employeeService.updateEmployee(employee);
        clearForm();
    }
    public void createEmployee() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
        Date date =sdf.parse((String)birthDate);
        EmployeeDTO employee = new EmployeeDTO();
        employee.setName(name);
        employee.setBirthDate(date);
        employee.setRole(AdminRole.valueOf(role));
        employee.setPassword(password);
        employeeService.createEmployee(employee);
        clearForm();
    }
    public void loadEmployee(Long id) {
        this.id=id;
        EmployeeDTO employee = employeeService.getEmployeeById(id);
        if(employee != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
            name=employee.getName();
            birthDate=sdf.format(employee.getBirthDate());
            role=employee.getRole().toString();
            password=employee.getPassword();
        }
    }
    
    public void clearForm() {
        id=null;
        name=null;
        birthDate=null;
        password=null;
        role=null;
    }
    
}
